package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.Autofill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFloavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMApper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
   @Autowired
   private DishFloavorMapper dishFloavorMapper;
   @Autowired
   private SetmealDishMApper setmealDishMApper;
    //新增菜品
    @Transactional
    @Override
    public void add(DishDTO dto) {
        //向菜品表插入数据
        Dish dish=new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.add(dish);
        //向口味插入数据
        Long dishID=dish.getId();

        List<DishFlavor> flavors=dto.getFlavors();
        if(flavors !=null && flavors.size()>0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishID);
            });
                  dishFloavorMapper.add(flavors);
        }


    }

    @Override
    public PageResult select(DishPageQueryDTO dishPageQueryDTO) {
        //开始动态分页查询自动将limit拼接和动态分页参数
    PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

   Page<DishVO> page=dishMapper.select(dishPageQueryDTO);
   return new PageResult(page.getTotal(), page.getResult());

    }
@Transactional
    @Override
    public void delete(List<Long> ids)  {
     //判断菜品是否起售停售
       for (Long id:ids){
   Dish dish=  dishMapper.getById(id);
           if(dish.getStatus()== StatusConstant.ENABLE){
               //起售不允许删除
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
           }
       }
    //判断菜品是否与套餐关联
        List<Long> setemealIdByDishIds = setmealDishMApper.getSetemealIdByDishIds(ids);
if(setemealIdByDishIds!=null&&setemealIdByDishIds.size()>0){
    throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
}
        //可以删除菜品
      /*  for (Long id:ids) {
            dishMapper.delete(id);
            //删除关联的口味
            dishFloavorMapper.deleteByDishID(id);
        }*/
    //批量删除菜品
    dishMapper.deletes(ids);
        //批量删除关联的口味
    dishFloavorMapper.deleteDishId(ids);


    }

    @Override
    public DishVO getById(Long id) {
        DishVO dishVO=new DishVO();
        Dish dish=dishMapper.getById(id);
        List<DishFlavor> dishFlavors= dishFloavorMapper.getByflavor(id);
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Autofill(OperationType.UPDATE)
    @Override
    public void updata(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishFloavorMapper.deleteByDishID(dishDTO.getId());

        List<DishFlavor> dishFlavors=dishDTO.getFlavors();
        if(dishFlavors!=null&&dishFlavors.size()>0){
        dishFlavors.forEach(d->{
            d.setDishId(dishDTO.getId());
        });
        //添加口味
            dishFloavorMapper.add(dishFlavors);
        }

    }

    @Override
    public void ststus(Integer status, Long id) {
        Dish dish=new Dish();
        dish.setId(id);
        dish.setStatus(status);

        if (status == StatusConstant.DISABLE) {
            // 如果是停售操作，还需要将包含当前菜品的套餐也停售
            List<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            // select setmeal_id from setmeal_dish where dish_id in (?,?,?)
            List<Long> setmealIds = setmealDishMApper.getSetemealIdByDishIds(dishIds);//setmealDishMApper
            if (setmealIds != null && setmealIds.size() > 0) {
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealDishMApper.update(setmeal);
                }
            }
        }

    dishMapper.status(dish);
    }





    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */    @Override

    public List<DishVO> listwhitflavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);
            //dishFlavorMapper
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFloavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

}
